package com.ctshk.rpc.order.custom.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ctshk.rpc.order.custom.base.CustomBaseMapper;
import com.ctshk.rpc.order.custom.dto.order.AddReceiptTouristDTO;
import com.ctshk.rpc.order.custom.dto.order.ReceiptTouristDTO;
import com.ctshk.rpc.order.custom.entity.CustomizedOrderCollection;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 游客收款 Mapper 接口
 * </p>
 *
 * @author 叶冠峰
 * @since 2021-02-22
 */
public interface CustomizedOrderCollectionMapper extends CustomBaseMapper<CustomizedOrderCollection> {


    /**
     * 订单收款统计
     * @param collectionTypeOther
     * @param orderId
     * @param collectionTypeId
     * @return
     */
    @Select("SELECT COUNT(id) AS peopleNum,SUM(paid_in_amount) AS receiptAmount \n" +
            "FROM t_customized_order_collection\n" +
            "WHERE order_id = #{order_id} AND collection_type_id != #{collection_type_id} AND collection_type_id != #{collection_type_other} AND `status` = 3 AND is_deleted = 0")
    AddReceiptTouristDTO queryAddDetailListByOrder(@Param("order_id") Long orderId, @Param("collection_type_id") Long collectionTypeId,@Param("collection_type_other") Long collectionTypeOther);

    /**
     * 旅客ID列表
     * @param orderId
     * @param collectionTypeId
     * @return
     */
    @Select("SELECT tourist_id,paid_in_amount \n" +
            "FROM t_customized_order_collection\n" +
            "WHERE order_id = #{order_id} AND collection_type_id != #{collection_type_id} AND collection_type_id != #{collection_type_other} AND `status` = 3 AND is_deleted = 0")
    List<ReceiptTouristDTO> queryTouristIdByOrder(@Param("order_id") Long orderId, @Param("collection_type_id")Long collectionTypeId,@Param("collection_type_other") Long collectionTypeOther);

    /**
     * 批量更新收款记录（插入receiptId）
     * @param orderId
     * @param receiptId
     * @param userId
     * @param collectionTypeId
     * @return
     */
    @Update("UPDATE t_customized_order_collection\n" +
            "SET receipt_id = #{receipt_id},modified_id = #{modified_id},is_print = 1\n" +
            "WHERE order_id = #{order_id} AND collection_type_id != #{collection_type_id} AND is_deleted = 0")
    Integer batchUpdateReceipt(@Param("order_id") Long orderId,@Param("receipt_id") Long receiptId, @Param("modified_id")Long userId,@Param("collection_type_id")Long collectionTypeId);

    /**
     * 查询旅客已使用优惠金额
     * @param touristId
     * @param orderId
     * @param promotionCode
     * @param bankDiscountCode
     * @return
     */
    @Select("SELECT SUM(paid_in_amount)\n" +
            "FROM t_customized_order_collection\n" +
            "WHERE tourist_id = #{tourist_id} AND order_id = #{order_id} AND is_deleted = 0\n" +
            "AND (collection_way_id = #{id1} OR collection_way_id = #{id2})")
    BigDecimal sumDiscountAmountByTourist(@Param("tourist_id") Long touristId, @Param("order_id") Long orderId, @Param("id1") Long promotionCode,@Param("id2") Long bankDiscountCode);
}
